﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SocialPhotos.Models.Entities;

namespace SocialPhotos.Models.SqlExpressRepository
{
    public class SqlExPhotoRepository : IPhotoRepository
    {
        /// <summary>
        /// SQL Server Express Context
        /// </summary>
        private readonly EfDbContext _dbContext = new EfDbContext();

        /// <summary>
        /// Photoses this instance.
        /// </summary>
        /// <returns></returns>
        public IQueryable<Photo> Photos()
        {
            return this._dbContext.Photos.Select(p => p).OrderByDescending(p=>p.Id);
        }

        /// <summary>
        /// Photoses the specified category.
        /// </summary>
        /// <param name="category">The category.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="page">The page.</param>
        /// <param name="size">The size.</param>
        /// <returns></returns>
        public IQueryable<Photo> Photos(int? category, int? userId, int page, int size)
        {
            IQueryable<Photo> query =
                this._dbContext.Photos.Where(
                    p =>
                    (!category.HasValue || p.Category.Id == category.Value) &&
                    (!userId.HasValue || p.User.Id == userId.Value)).OrderByDescending(p=>p.Id).Skip(page*size).Take(size);
            return query;
        }

        /// <summary>
        /// Gets the page number.
        /// </summary>
        /// <param name="category">The category.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="size">The size.</param>
        /// <returns></returns>
        public int GetPageNumber(int? category, int? userId, int size)
        {
            IQueryable<Photo> query =
                this._dbContext.Photos.Where(
                    p =>
                    (!category.HasValue || p.Category.Id == category.Value) &&
                    (!userId.HasValue || p.User.Id == userId.Value)).OrderByDescending(p => p.Id);
            return query.Count();
        }

        /// <summary>
        /// Gets the top rate.
        /// </summary>
        /// <param name="top">The top.</param>
        /// <param name="category">The category.</param>
        /// <returns></returns>
        public IQueryable<Photo> GetTopRate(int top, int category = 0)
        {
            return
                this._dbContext.Photos.Where(p => category == 0 || p.Category.Id == category)
                                                    .OrderByDescending(p => p.Likes).Take(top);
        }

        /// <summary>
        /// Gets the top latest.
        /// </summary>
        /// <param name="top">The top.</param>
        /// <param name="category">The category.</param>
        /// <returns></returns>
        public IQueryable<Photo> GetTopLatest(int top, int category = 0)
        {
            return
                this._dbContext.Photos.Where(p => category == 0 || p.Category.Id == category)
                                                    .OrderByDescending(p => p.Id).Take(top);
        }
    }
}
